package com.spring.servlet;

import javax.servlet.AsyncContext;
import javax.servlet.ServletException;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Description:
 *
 * @author lijie
 * @create 2019-01-20 17:44
 * Update by lijie 2019-01-20 17:44
 */
@WebServlet(value = {"/hello"},asyncSupported = true)
public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //super.doGet(req, resp);
        resp.getWriter().write("hello world");
        AsyncContext asyncContext = req.startAsync();
        asyncContext.start(new Runnable() {
            @Override
            public void run() {
                System.out.println("业务逻辑开始运行....");
                asyncContext.complete();
                AsyncContext asyncContext1 = req.getAsyncContext();
                ServletResponse response = asyncContext1.getResponse();
                try {
                    response.getWriter().write("hello world...");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
